package com.beordie.dao.impl;

import com.beordie.dao.INoticeDao;
import com.beordie.model.*;
import com.beordie.utils.DBUtils;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

/**
 * @Classname NoticeDaoImpl
 * @Description TODO
 * @Date 2021/7/14 11:18
 * @Created 30500
 */
public class NoticeDaoImpl extends DBUtils implements INoticeDao {
    @Override
    public List<Notice> getAll() {
        return null;
    }

    @Override
    public List<Notice> getAllByLimit(boolean isLimit, int start, int size) {
        return null;
    }

    @Override
    public int insert(Notice notice) {
        List<Object> params = new ArrayList<>();
        params.add(notice.getNumber());
        params.add(notice.getDay());
        params.add(notice.getTime());
        params.add(notice.getLocation());
        params.add(notice.getRecord());
        params.add(notice.getDriver().getId());
        params.add(notice.getPolice().getId());
        params.add(notice.getCar().getId());

        int result = 0;
        try {
            result = super.update(SQL_INSERT_ONE, params);
            if (result > 0) {
                resultSet = statement.getGeneratedKeys();
                while (resultSet.next()) {
                    // 获取 ID
                    result = resultSet.getInt(1);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            result = -1;
        } finally {
            super.closeAll();
        }
        return result;
    }

    @Override
    public int deleteById(int id) {
        List<Integer> params = new ArrayList<>();
        params.add(id);
        int result = 0;
        try {
            result = super.update(SQL_DELETE_BY_ID, params);
        } catch (Exception e) {
            e.printStackTrace();
            result = -1;
        } finally {
            super.closeAll();
        }
        return result;
    }

    @Override
    public int updateById(Notice notice, int id) {
        List<Object> params = new ArrayList<>();
        params.add(notice.getDay());
        params.add(notice.getTime());
        params.add(notice.getLocation());
        params.add(notice.getRecord());
        params.add(notice.getDriver().getId());
        params.add(notice.getPolice().getId());
        params.add(notice.getCar().getId());
        params.add(id);

        int result = 0;
        try {
            result = super.update(SQL_UPDATE_BY_ID, params);
        } catch (Exception e) {
            e.printStackTrace();
            result = -1;
        } finally {
            super.closeAll();
        }
        return result;
    }

    @Override
    public Notice findByNumber(String condition) {
        Notice notice = null;
        List<String> params = new ArrayList<>();
        params.add(condition);
        try {
            resultSet = super.query(SQL_SELECT_BY_NUMBER, params);
            if (resultSet.next()) {
                notice = new Notice();
                notice.setId(resultSet.getInt("id"));
                notice.setNumber(resultSet.getString("number"));
                notice.setDay(resultSet.getDate("day"));
                notice.setTime(resultSet.getTime("time"));
                notice.setRecord(resultSet.getString("record"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            super.closeAll();
        }
        return notice;
    }

    @Override
    public int getCount() {
        return 0;
    }

    @Override
    public Notice getByKey(String number, String way) {
        List<String> params = new ArrayList<>();
        params.add(number);
        Notice notice = null;
        Car car = null;
        Driver driver = null;
        Police police = null;

        try {
            if ("1".equals(way))
                resultSet = super.query(SQL_SELECT_BY_DRIVER, params);
            else if ("2".equals(way))
                resultSet = super.query(SQL_SELECT_BY_CAR, params);
            else if ("3".equals(way))
                resultSet = super.query(SQL_SELECT_BY_POLICE, params);
            else
                resultSet = super.query(SQL_SELECT_BY_KEY, params);
            if (resultSet.next()) {
                notice = new Notice();
                car = new Car();
                driver = new Driver();
                police = new Police();

                driver.setName(resultSet.getString("name"));
                driver.setNumber(resultSet.getString("number"));
                driver.setAddress(resultSet.getString("address"));
                driver.setPostCard(resultSet.getString("postcard"));
                driver.setPhone(resultSet.getString("phone"));
                car.setNumber(resultSet.getString("cnumber"));
                car.setModel(resultSet.getString("model"));
                car.setFactory(resultSet.getString("factory"));
                car.setCreateTime(resultSet.getDate("createtime"));
                notice.setId(resultSet.getInt("id"));
                notice.setDay(resultSet.getDate("day"));
                notice.setTime(resultSet.getTime("time"));
                notice.setRecord(resultSet.getString("record"));
                notice.setLocation(resultSet.getString("location"));
                police.setName(resultSet.getString("pname"));
                police.setNumberPolice(resultSet.getString("numberpolice"));

                notice.setDriver(driver);
                notice.setCar(car);
                notice.setPolice(police);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            super.closeAll();
        }
        return notice;
    }
}
